using System.Collections;
using UnityEngine;

public class ObjectFalls : MonoBehaviour
{
	public Rigidbody rigidbody;

	public BoxCollider[] colliders;

	public MeshRenderer meshRenderer;

	public GameObject[] wires;

	private bool isFalls;

	private Vector3 startPos;

	private Quaternion startRot;

	private void Awake()
	{
		startPos = base.transform.localPosition;
		startRot = base.transform.localRotation;
		rigidbody.mass = 50f;
		BoxCollider[] array = colliders;
		foreach (BoxCollider boxCollider in array)
		{
			if (boxCollider.isTrigger)
			{
				boxCollider.size = new Vector3(2f, 10f, 2f);
			}
		}
		base.gameObject.layer = 27;
	}

	private void OnEnable()
	{
		base.transform.localPosition = startPos;
		base.transform.localRotation = startRot;
		rigidbody.isKinematic = true;
		SetState(true);
		isFalls = false;
		for (int i = 0; i < wires.Length; i++)
		{
			if (wires[i] != null)
			{
				wires[i].SetActive(true);
			}
		}
	}

	private void OnCollisionEnter(Collision collision)
	{
		if (isFalls || !(collision.collider.tag == "MyVehicle"))
		{
			return;
		}
		rigidbody.useGravity = true;
		rigidbody.AddForce(collision.impulse);
		isFalls = true;
		StartCoroutine(WaitRemoval());
		for (int i = 0; i < wires.Length; i++)
		{
			if (wires[i] != null)
			{
				wires[i].SetActive(false);
			}
		}
	}

	private void OnTriggerEnter(Collider other)
	{
		if (!isFalls && other.tag == "MyVehicle")
		{
			rigidbody.isKinematic = false;
			rigidbody.useGravity = false;
		}
	}

	private void OnTriggerExit(Collider other)
	{
		if (!isFalls && other.tag == "MyVehicle")
		{
			rigidbody.isKinematic = true;
		}
	}

	private IEnumerator WaitRemoval()
	{
		yield return new WaitForSeconds(5f);
		SetState(false);
	}

	private void SetState(bool enable)
	{
		meshRenderer.enabled = enable;
		BoxCollider[] array = colliders;
		foreach (BoxCollider boxCollider in array)
		{
			boxCollider.enabled = enable;
		}
		rigidbody.isKinematic = true;
	}
}
